﻿Imports System.ComponentModel
Imports System.Windows.Forms

Public Class frmBuildMapping

    Public Property dictMapping As Dictionary(Of String, clsMapping)
    Public Property Project As WRDB.Project.clsProject = Nothing

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
        Static processing As Boolean = False
        If Not processing Then
            Dim parent As TreeNode = e.Node.Parent
            If parent Is Nothing Then 'check children
                For Each tn As TreeNode In e.Node.Nodes
                    processing = True
                    tn.Checked = e.Node.Checked
                    SelectNode(tn) 'to clear or set default stationid & pcode
                    processing = False
                Next
            Else
                Dim numchecked As Integer = 0
                For Each tn As TreeNode In parent.Nodes
                    If tn.Checked Then numchecked += 1
                Next
                'treeview does not allow tri-state checks; if any are checked, check parent
                processing = True
                parent.Checked = numchecked > 0
                processing = False
                TreeView1.SelectedNode = e.Node
                SelectNode(e.Node)
            End If
            If e.Node.Nodes.Count = 0 Then SelectNode(e.Node)
        End If
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        SelectNode(e.Node)
    End Sub

    Private Sub SelectNode(node As TreeNode)
        Dim DataID As String = node.Text
        If node.Checked AndAlso node.Nodes.Count = 0 Then 'is terminal node, can display station id and pcode
            cboStationID.Enabled = True
            cboPCode.Enabled = True
            txtDepth.Enabled = True
            cboLCode.Enabled = True
            Dim StationID As String = ""
            Dim PCode As String = ""
            Dim Depth As Double = Double.NaN
            Dim LCode As String = ""
            If dictMapping.ContainsKey(DataID) Then
                StationID = dictMapping(DataID).StationID.ToUpper
                PCode = dictMapping(DataID).PCode.ToUpper
                Depth = dictMapping(DataID).Depth
                LCode = dictMapping(DataID).LCode.ToUpper
            End If
            If DataID.Contains("@") Then 'is Aquarius ID
                If PCode = "" Then PCode = Strings.Left(DataID.Split(".")(0), 10).Trim.ToUpper.Replace(" ", "_")
                If StationID = "" Then StationID = Strings.Left(DataID.Split("@")(1), 25).Trim.ToUpper.Replace(" ", "_")
            ElseIf DataID.Contains(".") Then 'is SCADA ID
                If PCode = "" Then PCode = Strings.Left(DataID.Split(".")(1), 10).Trim.ToUpper.Replace(" ", "_")
                If StationID = "" Then StationID = Strings.Left(DataID.Split(".")(1), 25).Trim.ToUpper.Replace(" ", "_")
            ElseIf DataID.Contains("/") Then 'is Hydrlynx ID
                If PCode = "" Then PCode = Strings.Left(DataID.Split("/")(1), 10).Trim.ToUpper.Replace(" ", "_")
                If StationID = "" Then StationID = Strings.Left(DataID.Split("/")(0), 10).Trim.ToUpper.Replace(" ", "_")
            End If
            If dictMapping.ContainsKey(DataID) Then dictMapping.Remove(DataID)
            dictMapping.Add(DataID, New clsMapping With {.StationID = StationID, .PCode = PCode, .Depth = Depth, .LCode = LCode})
            cboStationID.Text = StationID
            cboPCode.Text = PCode
            txtDepth.Text = TestNull(Depth, "")
            cboLCode.Text = LCode
        Else
            cboStationID.Enabled = False
            cboPCode.Enabled = False
            txtDepth.Enabled = False
            cboLCode.Enabled = False
            If dictMapping.ContainsKey(DataID) Then dictMapping.Remove(DataID)
            cboStationID.Text = ""
            cboPCode.Text = ""
            txtDepth.Text = ""
            cboLCode.Text = ""
        End If
    End Sub

    Private Sub cboStationID_Validating(sender As Object, e As CancelEventArgs) Handles cboStationID.Validating
        cboStationID.Text = Strings.Left(cboStationID.Text, 25).Trim.ToUpper.Replace(" ", "_")
    End Sub

    Private Sub cboPCode_Validating(sender As Object, e As CancelEventArgs) Handles cboPCode.Validating
        cboPCode.Text = Strings.Left(cboPCode.Text, 10).Trim.ToUpper.Replace(" ", "_")
    End Sub

    Private Sub txtDepth_Validating(sender As Object, e As CancelEventArgs) Handles txtDepth.Validating
        txtDepth.Text = txtDepth.Text.Trim
        If txtDepth.Text <> "" AndAlso Not IsNumeric(txtDepth.Text) Then e.Cancel = True
    End Sub

    Private Sub cboLCode_Validating(sender As Object, e As CancelEventArgs) Handles cboLCode.Validating
        cboLCode.Text = Strings.Left(cboLCode.Text, 3).Trim.ToUpper.Replace(" ", "_")
    End Sub

    Private Sub txt_Validated(sender As Object, e As EventArgs) Handles cboStationID.Validated, cboPCode.Validated, txtDepth.Validated, cboLCode.Validated
        If TreeView1.SelectedNode.Checked Then
            Dim DataID As String = TreeView1.SelectedNode.Text
            cboStationID.Text = cboStationID.Text.ToUpper
            cboPCode.Text = cboPCode.Text.ToUpper
            cboLCode.Text = cboLCode.Text.ToUpper
            If dictMapping.ContainsKey(DataID) Then
                dictMapping(DataID) = New clsMapping With {.StationID = cboStationID.Text, .PCode = cboPCode.Text, .Depth = TestNull(txtDepth.Text.Trim, Double.NaN), .LCode = cboLCode.Text}
            End If
        End If
    End Sub

    Private Sub mnu_Click(sender As Object, e As EventArgs) Handles mnuAll.Click, mnuNone.Click
        For Each tn As TreeNode In TreeView1.Nodes
            tn.Checked = sender Is mnuAll
            SelectNode(tn) 'to update the mappings
        Next
    End Sub

    Private Sub frmBuildMapping_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Project IsNot Nothing Then
            cboStationID.Items.AddRange(Project.DB.GetRecordList("Stations", "Station_ID").ToArray)
            cboPCode.Items.AddRange(Project.DB.GetRecordList("PCodes", "PCode").ToArray)
            cboLCode.Items.AddRange(Project.DB.GetRecordList("Validation", "Valid_Value", "WHERE Field_Name='LCODE'").ToArray)
        End If
    End Sub

End Class

Public Class clsMapping
    Public Property StationID As String
    Public Property PCode As String
    Public Property Depth As Double
    Public Property LCode As String
End Class

